
## 1. load most recent data
exp_main <- read.csv(here("data/main_anon.csv"))

# 2. pivot topic longer
main_long <-
    exp_main %>%
    filter(!is.na(submitdate)) %>% # filter submitted answers
    pivot_longer(
        cols = contains("SQ001"),
        names_to = "issue",
        names_pattern = "(.*)att\\.SQ001\\.",
        values_to = "attitude"
    ) %>%
    mutate(
        recall_number = 
            case_when(
                recall == "Vermögenssteuer" ~ 1,
                recall == "Tempolimit" ~ 2,
                recall == "Schulpolitik" ~ 3,
                recall == "Asylrecht" ~ 4,
                recall == "Rentenversicherung" ~ 5
            ),
        issue_number = 
            case_when(
                issue == "waelth"    ~ 1,
                issue == "tempo"     ~ 2,
                issue == "education" ~ 3,
                issue == "asylum"    ~ 4,
                issue == "pension"   ~ 5
            ) 
    ) %>%
    filter(issue_number == topic)


## drop attention check failures ()
main_long <-
    main_long %>%
    filter(recall_number == topic)

## clean
main_long <-
    main_long %>%
    mutate(
        frame = ifelse(frame == 1, "pro", "con"),
        inpartytree = inpartytree == 1)

## define attitudinal variable t_2
main_long <- 
    main_long %>%
    pivot_longer(
        cols = contains("issueatt."),
        names_to = c("issue_att_t2"),
        names_pattern = "issueatt\\.(.*)\\.",
        values_to = "att_t2"
    ) %>%
    mutate(
        issue_att_t2 = 
            case_when(
                issue_att_t2 == "a" ~ 1,
                issue_att_t2 == "b" ~ 2,
                issue_att_t2 == "c" ~ 3,
                issue_att_t2 == "d" ~ 4,
                issue_att_t2 == "e" ~ 5
            )
    ) %>%
    filter(issue_att_t2 == topic) %>%
    mutate(delta_att = att_t2 - attitude) %>%
    mutate(att_ceil = (attitude == 100 | attitude == 0))


## define party position
main_long <- 
    main_long %>%
    mutate(
        party_pos =
            case_when(
                ## spd
                partytree == 1 & topic == 1 ~ "pro",
                partytree == 1 & topic == 2 ~ "pro",
                partytree == 1 & topic == 3 ~ "pro",
                partytree == 1 & topic == 4 ~ "con",
                partytree == 1 & topic == 5 ~ "pro",
                partytree == 1 & topic == 6 ~ "pro",
                partytree == 1 & topic == 7 ~ "con",

                ## cdu
                partytree == 2 & topic == 1 ~ "con",
                partytree == 2 & topic == 2 ~ "con",
                partytree == 2 & topic == 3 ~ "con",
                partytree == 2 & topic == 4 ~ "con",
                partytree == 2 & topic == 5 ~ "con",
                partytree == 2 & topic == 6 ~ "con",
                partytree == 2 & topic == 7 ~ "pro",

                ## grüne
                partytree == 3 & topic == 1 ~ "pro",
                partytree == 3 & topic == 2 ~ "pro",
                partytree == 3 & topic == 3 ~ "pro",
                partytree == 3 & topic == 4 ~ "pro",
                partytree == 3 & topic == 5 ~ "pro",
                partytree == 3 & topic == 6 ~ "pro",
                partytree == 3 & topic == 7 ~ "con",

                ## fdp
                partytree == 4 & topic == 1 ~ "con",
                partytree == 4 & topic == 2 ~ "con",
                partytree == 4 & topic == 3 ~ "pro",
                partytree == 4 & topic == 4 ~ "con",
                partytree == 4 & topic == 5 ~ "con",
                partytree == 4 & topic == 6 ~ "con",
                partytree == 4 & topic == 7 ~ "pro",

                ## afd
                partytree == 5 & topic == 1 ~ "con",
                partytree == 5 & topic == 2 ~ "con",
                partytree == 5 & topic == 3 ~ "con",
                partytree == 5 & topic == 4 ~ "con",
                partytree == 5 & topic == 5 ~ "pro",
                partytree == 5 & topic == 6 ~ "con",
                partytree == 5 & topic == 7 ~ "pro",

                ## linke
                partytree == 6 & topic == 1 ~ "pro",
                partytree == 6 & topic == 2 ~ "pro",
                partytree == 6 & topic == 3 ~ "pro",
                partytree == 6 & topic == 4 ~ "pro",
                partytree == 6 & topic == 5 ~ "pro",
                partytree == 6 & topic == 6 ~ "pro",
                partytree == 6 & topic == 7 ~ "con",

                ## else
                TRUE ~ NA_character_
            )
        )

## get general party positions
party_positions <- 
    main_long %>%
    group_by(topic, partytree) %>%
    summarise(party_pos_inparty = unique(party_pos)) %>%
    mutate(inparty = partytree) %>%
    dplyr::select(-partytree)

## assign inparty positions
main_long <- 
    main_long %>%
    left_join(party_positions, by = c("topic", "inparty"))

## generate common ptv
main_long <- 
    main_long %>%
    pivot_longer(
        cols = contains("ptv."),
        names_to = "party",
        names_pattern = "ptv\\.(.*)\\.",
        values_to = "ptv_treated"
    ) %>%
    filter(party == partytree) %>%
    dplyr::select(id, ptv_treated) %>%
    right_join(main_long, by = "id") %>%
    mutate(frame_to_party = frame == party_pos)

main_long <- 
    main_long %>%
    mutate(
        partyvote = 
            case_when(
                vote == "SPD" & partytree == "1" ~ T,
                vote == "CDU/CSU" & partytree == "2" ~ T,
                vote == "Die Grünen" & partytree == "3" ~ T,
                vote == "FDP" & partytree == "4" ~ T,
                vote == "AfD" & partytree == "5" ~ T,
                vote == "Die Linke" & partytree == "6" ~ T,
                T ~ F
            )
    )

## recode attitudinal change to be directed towards party Position
main_long <- 
    main_long %>%
    mutate(
        delta_att_inparty = 
            ifelse(
                party_pos_inparty == "con", 
                delta_att*-1, 
                delta_att),
        delta_att_party =
            ifelse(
                party_pos == "con",
                delta_att*-1,
                delta_att)
                )

## generate polknowl index
main_long <- 
    main_long %>%
        mutate(
            firstsecondvote = ifelse(firstsecondvote == "Zweitstimme", 1, 0),
            votingtreshold = ifelse(votingtreshold == "5%", 1, 0),
            electiondate = ifelse(electiondate == "Alle 4 Jahre", 1, 0),
            bundesrat.a. = ifelse(bundesrat.a. == "Ja", 1, 0),
            bundesrat.b. = ifelse(bundesrat.b. == "Nein", 1, 0),
            bundesrat.c. = ifelse(bundesrat.c. == "Ja", 1, 0),
            bundesrat.d. = ifelse(bundesrat.d. == "Ja", 1, 0),
            )
            
main_long$pk_idx <- 
    factanal(
        main_long %>%
        select(
            firstsecondvote,
            votingtreshold, 
            electiondate,
            contains("bundesrat") & !bundesratTime
        ), 
    factors = 1, 
    scores = "regression")$scores

## generate issuesalience variable
main_long$salience = 
    case_when(
        main_long$topic == 1 ~ main_long$issuesalience.a.,
        main_long$topic == 2 ~ main_long$issuesalience.b.,
        main_long$topic == 3 ~ main_long$issuesalience.c.,
        main_long$topic == 4 ~ main_long$issuesalience.d.,
        main_long$topic == 5 ~ main_long$issuesalience.e.,
        TRUE ~ NA_real_
    )

## write to csv
write.csv(main_long, here("data/main_long_clean.csv"), row.names = FALSE)